<template>
  <el-select v-model="value" @change="change($event)" filterable clearable class="elSelect" :placeholder="tip">
    <el-option
      v-for="item in drivers"
      :key="item.id"
      :label="item.driverName+' '+ item.phone"
      :value="item.id"
    >
    </el-option>
  </el-select>
</template>
<script>

import {getList} from "@/api/ems/driver";
export default {
  props: {
        defval:{type:String,default:""},
        deptId:{type:String,default:""},
        tip:{type:String,default:"请选择司机"},
        search:{type:Object,default: function () { return [] } },
    },
  data() {
    return {
      drivers: [],
      value: "",
    };
  },
  created() {
    this.getList(true)
  },
   watch: {
    deptId() {
      this.getList(false);
    },
  },
  methods: {
    getList(isFirst){
      //根据传递的车队，获取指定车队的驾驶员
      if(this.deptId!="") this.search['deptId']=this.deptId;
      getList(1,1000,this.search).then((res) => {
        this.drivers = res.data.data.records;
        if(isFirst){
          
          if(this.defval!="") {
            let item = this.drivers.find((e) => {
              return e.id === this.defval; //筛选出匹配数据
             });
            if(item) this.value=this.defval;
          }
        }else{
            let v="";//清空
            this.value=v;
            this.change(v)
        }
        
      });
    },
    change (v) {
      let item = this.drivers.find((e) => {
        return e.id === v; //筛选出匹配数据
      });
      let o={value:v,info:item}
      this.$emit('change', o)
    },
    reset(){
        this.value=this.defval;
    }
  },
};
</script>

